package com.xyxy.web;

import com.xyxy.pojo.Book;
import com.xyxy.pojo.Page;
import com.xyxy.service.BookService;
import com.xyxy.service.impl.BookServiceImpl;
import com.xyxy.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

public class BookServlet extends BaseServlet {

    private BookService bookService = new BookServiceImpl();


    protected void add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"),0);
        pageNo += 1;

        //1,获取请求参数，封装成javabean对象
        Book book = WebUtils.copyParamToBean(req.getParameterMap(), new Book());
        //2,调用bookService.addBook()方法
        bookService.addBook(book);
        //3,重定向到图书列表页面/manager/bookServlet?action=list
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + pageNo);
    }


    protected void getBook(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1,获取请求的参数图书编号
        int id = WebUtils.parseInt(req.getParameter("id"), 0);
        //2,用bookService.queryBookById查询图书
        Book book = bookService.queryBookById(id);
        //3,保存到图书到 Request 域
        req.setAttribute("book",book);
        //4,请求转发到pages/manager/book_edit.jsp页面
        req.getRequestDispatcher("/pages/manager/book_edit.jsp").forward(req,resp);

    }

    protected void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.获取请求的参数==封装成为Book对象
        Book book = WebUtils.copyParamToBean(req.getParameterMap(), new Book());
        //2,调用BookService.updateBook()修改图书
        bookService.updateBook(book);
        //3,重定向回图书列表管理页面  /工程名/manager/bookServlet?action=list
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }

    protected void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1,获取请求的参数图书id
        int id = WebUtils.parseInt(req.getParameter("id"),0);
        //2,调用service层删除书籍
        bookService.deleteBookById(id);
        //3,重定向回/book/manager/bookServlet?action=list
        resp.sendRedirect(req.getContextPath() + "/manager/bookServlet?action=page&pageNo=" + req.getParameter("pageNo"));
    }

    //查询全部图书
    protected void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1,通过bookService查询全部图书
        List<Book> books = bookService.queryBooks();
        //2,将图书保存到request域中
        req.setAttribute("books",books);
        //3,请求转发到/pages/manager/book_manager.jsp页面
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }

    //处理分页
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //1,获取请求的参数pageNo和pageSize
        int pageNo = WebUtils.parseInt(req.getParameter("pageNo"),1);
        int pageSize = WebUtils.parseInt(req.getParameter("pageSize"), Page.PAGE_SIZE);

        //2,调用BookService.page(pageNo，pageSize)：Page对象
        Page<Book> page = bookService.page(pageNo,pageSize);

        //设置分页条的请求地址
        page.setUrl("manager/bookServlet?action=page");

        //3,保存Page对象到Request域中
        req.setAttribute("page",page);

        //4,请求转发到pages/manager/book_manager.jsp页面
        req.getRequestDispatcher("/pages/manager/book_manager.jsp").forward(req,resp);
    }
}
